package com.zrwl.poscloud.core.restapi.admin.baseInfo;


import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.CompanyService;
//import com.zrwl.poscloud.xo.service.FrontServerService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.CompanyVO;
//import com.zrwl.poscloud.xo.vo.FrontServerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 商户表 RestApi
 *
 * @author liusitong
 * @date 2020年1月4日21:29:09
 */
@RestController
@Api(value = "商户相关接口", tags = {"商户相关接口"})
@RequestMapping("/company")
@Slf4j
public class CompanyRestApi {

    @Autowired
    private CompanyService companyService;
//
//    @Autowired
//    private FrontServerService frontServerService;
    @Autowired
    private AdminService adminService;

    @AuthorityVerify
    @ApiOperation(value = "获取商户列表", notes = "获取商户列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody CompanyVO CompanyVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取商户列表: {}", CompanyVO);
        return ResultUtil.successWithData(companyService.getPageList(CompanyVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取当前商户", notes = "获取当前商户", response = String.class)
    @PostMapping("/getCurrentCompany")
    public String getCurrentCompany(@Validated({GetList.class}) @RequestBody CompanyVO CompanyVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取当前商户: {}", CompanyVO);
        return ResultUtil.successWithData(companyService.getCurrentCompany(CompanyVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取商户前置服务器列表", notes = "获取商户前置服务器列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody CompanyVO CompanyVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取商户前置服务器列表: {}", CompanyVO);
        return ResultUtil.successWithData(companyService.getAllList(CompanyVO));
    }

//    @AuthorityVerify
//    @OperationLogger(value = "新增商户")
//    @ApiOperation(value = "新增商户", notes = "新增商户", response = String.class)
//    @PostMapping("/add")
//    public String add(@Validated({Insert.class}) @RequestBody CompanyVO CompanyVO, BindingResult result) throws Exception {
//
//        // 参数校验
//        ThrowableUtils.checkParamArgument(result);
//        log.info("新增商户: {}", CompanyVO);
//        AdminVO adminVO = adminService.getMe();
//        if (!adminVO.isMerchant()) {
//            return ResultUtil.errorWithMessage("仅代理商用户支持此功能");
//        }
//        if (CompanyVO.getMerchantUid() == null) {
//            CompanyVO.setMerchantUid(adminVO.getMerchantUid());
//        }
//        return companyService.addCompany(CompanyVO);
//    }

//    @AuthorityVerify
//    @OperationLogger(value = "编辑商户")
//    @ApiOperation(value = "编辑商户", notes = "编辑商户", response = String.class)
//    @PostMapping("/edit")
//    public String edit(@Validated({Update.class}) @RequestBody CompanyVO CompanyVO, BindingResult result) {
//        // 参数校验
//        ThrowableUtils.checkParamArgument(result);
//        log.info("编辑商户: {}", CompanyVO);
//        return companyService.editCompany(CompanyVO);
//    }

//    @AuthorityVerify
//    @OperationLogger(value = "删除商户")
//    @ApiOperation(value = "删除商户", notes = "删除商户", response = String.class)
//    @PostMapping("/delete")
//    public String delete(@Validated({Delete.class}) @RequestBody CompanyVO CompanyVO, BindingResult result) {
//
//        // 参数校验
//        ThrowableUtils.checkParamArgument(result);
//        log.info("删除商户: {}", CompanyVO);
//        return companyService.deleteCompany(CompanyVO);
//    }
//
//    /***
//     * 前置服务器
//     * @param frontServerVO
//     * @param result
//     * @return
//     */
//    @AuthorityVerify
//    @ApiOperation(value = "获取商户前置服务器列表", notes = "获取商户前置服务器列表", response = String.class)
//    @PostMapping("/getFrontServerList")
//    public String getFrontServerList(@Validated({GetList.class}) @RequestBody FrontServerVO frontServerVO, BindingResult result) {
//
//        // 参数校验
//        ThrowableUtils.checkParamArgument(result);
//        log.info("获取商户前置服务器列表: {}", frontServerVO);
//        return ResultUtil.successWithData(frontServerService.getFrontServerList(frontServerVO));
//    }
//
//    @AuthorityVerify
//    @OperationLogger(value = "新增商户前置服务器")
//    @ApiOperation(value = "新增商户前置服务器", notes = "新增商户前置服务器", response = String.class)
//    @PostMapping("/addFrontServer")
//    public String addFrontServer(@Validated({Insert.class}) @RequestBody FrontServerVO frontServerVO, BindingResult result) {
//
//        // 参数校验
//        ThrowableUtils.checkParamArgument(result);
//        log.info("新增商户前置服务器: {}", frontServerVO);
//        return frontServerService.addFrontServer(frontServerVO);
//    }
//
//    @AuthorityVerify
//    @OperationLogger(value = "编辑商户前置服务器")
//    @ApiOperation(value = "编辑商户前置服务器", notes = "编辑商户前置服务器", response = String.class)
//    @PostMapping("/editFrontServer")
//    public String editFrontServer(@Validated({Update.class}) @RequestBody FrontServerVO frontServerVO, BindingResult result) {
//        // 参数校验
//        ThrowableUtils.checkParamArgument(result);
//        log.info("编辑商户前置服务器: {}", frontServerVO);
//        return frontServerService.editFrontServer(frontServerVO);
//    }
//
//    @AuthorityVerify
//    @OperationLogger(value = "删除商户前置服务器")
//    @ApiOperation(value = "删除商户前置服务器", notes = "删除商户前置服务器", response = String.class)
//    @PostMapping("/deleteFrontServer")
//    public String deleteFrontServer(@Validated({Delete.class}) @RequestBody FrontServerVO frontServerVO, BindingResult result) {
//
//        // 参数校验
//        ThrowableUtils.checkParamArgument(result);
//        log.info("删除商户前置服务器: {}", frontServerVO);
//        return frontServerService.deleteFrontServer(frontServerVO);
//    }
//
//    /***
//     * 经营场所级联列表
//     * @param companyVO
//     * @param result
//     * @return
//     */
//    @AuthorityVerify
//    @ApiOperation(value = "获取经营场所级联列表", notes = "获取经营场所级联列表", response = String.class)
//    @PostMapping("/getHouseList")
//    public String getHouseList(@Validated({GetList.class}) @RequestBody CompanyVO companyVO, BindingResult result) {
//        // 参数校验
//        ThrowableUtils.checkParamArgument(result);
//        log.info("获取经营场所级联列表: {}", companyVO);
//        return ResultUtil.successWithData(companyService.getHouseList(companyVO));
//    }

    @AuthorityVerify
    @ApiOperation(value = "获取当前商户信息", notes = "获取当前商户信息", response = String.class)
    @PostMapping("/getPresentCompanyMes")
    public String getPresentCompanyMes(@Validated({GetList.class}) @RequestBody CompanyVO CompanyVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取当前商户信息: {}", CompanyVO);
        return ResultUtil.successWithData(companyService.getPresentCompanyMes(CompanyVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "通过Uid获取商户信息", notes = "通过Uid获取商户信息", response = String.class)
    @PostMapping("/getCompanyMesByUid")
    public String getCompanyMesByUid(@Validated({GetList.class}) @RequestBody CompanyVO CompanyVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("通过Uid获取商户信息: {}", CompanyVO);
        return ResultUtil.successWithData(companyService.getCompanyMesByUid(CompanyVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "通过code和name获取商户信息", notes = "通过code和name获取商户信息", response = String.class)
    @PostMapping("/getCompanyMesByCodeAndName")
    public String getCompanyMesByCodeAndName(@Validated({GetList.class}) @RequestBody CompanyVO companyVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("通过Uid获取商户信息: {}", companyVO);
        return ResultUtil.successWithData(companyService.getCompanyMesByCodeAndName(companyVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取当前商户的基本信息", notes = "获取当前商户的基本信息", response = String.class)
    @PostMapping("/getCurrentCompanyInfo")
    public String getCurrentCompanyInfo(@Validated({GetList.class}) @RequestBody CompanyVO CompanyVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("通过Uid获取商户信息: {}", CompanyVO);
        return ResultUtil.successWithData(companyService.getCurrentCompanyInformation());
    }

//    @AuthorityVerify
    @ApiOperation(value = "服务器获取对应的商户信息", notes = "服务器获取对应的商户信息", response = String.class)
    @RequestMapping("/serverInitializationCompanyData")//加解密操作保证数据安全！！！
    public String serverInitializationCompanyData(@RequestParam("companyCode") String companyCode) {
        log.info("服务器获取对应的商户信息: {}", companyCode);
        return ResultUtil.successWithData(companyService.getByCode(companyCode));
    }
}